/***************************************************************************************************/
/* @date on 2020/08/28                                                                             */
/* @author Xie Jian                                                                                */
/* @email 190954950@qq.com                                                                         */
/* @describe 文件夹处理工具                                                                         */
/***************************************************************************************************/
use std::fs;

/// 目录是否存在
///
/// # Examples
///
/// ```
/// let b = is_exist("路径"); //b 返回是否存在文件夹
/// ```
pub fn is_exist(path: &str) -> bool {
    let f = fs::read_dir(path); //.unwrap();
    match f {
        Ok(_file) => {
            return true;
        }
        Err(_error) => {
            return false;
        }
    }
}

/// 创建目录
///
/// # Examples
///
/// ```
/// let b = create("目录"); //b 返回是否创建成功
/// ```
pub fn create(path: &str) -> bool {
    let is_exist_dir = is_exist(path);
    if !is_exist_dir {
        let f = fs::create_dir(path); //.unwrap();
        match f {
            Ok(_file) => {
                return true;
            }
            Err(_error) => {
                return false;
            }
        }
    } else {
        return false;
    }
}

/// 删除目录
///
/// # Examples
///
/// ```
/// let b = create("目录"); //b 返回是否删除成功
/// ```
pub fn delete(path: &str) -> bool {
    let is_exist_dir = is_exist(path);
    if is_exist_dir {
        let f = fs::remove_dir(path); //.unwrap();
        match f {
            Ok(_file) => {
                return true;
            }
            Err(_error) => {
                return false;
            }
        }
    } else {
        return false;
    }
}
